<%@ page contentType="text/html; charset=UTF-8" language="java"%>
<%@ page import="com.hzqy.commons.utils.ConstantUtils" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>组织用户增加</title>
<link rel="stylesheet" type="text/css"
	href="../../static/js/themes/defalut.css">
<link rel="stylesheet" type="text/css"
	href="../../static/js/themes/public.css">
<link rel="stylesheet" type="text/css"
	href="../../static/js/themes/ddspstyle.css">
<link rel="stylesheet" type="text/css"
	href="../../static/js/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="../../static/js/themes/icon.css">
<script type="text/javascript" src="../../static/js/jquery.min.js"></script>
<script type="text/javascript" src="../../static/js/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="../../static/js/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="../../static/js/jquery.validate.js"></script>
<script type="text/javascript" src="../../static/js/jquery.md5.js"></script>
<script type="text/javascript" src="../../static/js/index.js"></script>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<%
	String para =request.getParameter("f_po_id");
	int f_po_id = 0;
	if(para != null)
		f_po_id = Integer.valueOf(para);
	String []nonceObj=ConstantUtils.getNonceObject();
%>
<script type="text/javascript">
var f_po_id = <%=f_po_id%>;
	$(function() {
		$("#rightRole").empty();
		$("#leftRole").empty();
		var leftStr="";
		var rightStr="";
		$.ajax({
			url:"../../pms/orgRole_list.do",
			success:function(data) {
				data = JSON.parse(data);
				$.ajax({
					url:"../user/orgRole_select.do",
					success:function(roleData) {
						roleData = JSON.parse(roleData);
						for(var i=0;i<data.length;i++) {
							var roleIsExist = false;
							for(var j=0;j<roleData.length;j++) {
								if(data[i].f_prole_id == roleData[j].f_por_roleId){
									roleIsExist = true;
								}
							}
							if(roleIsExist)
								leftStr+='<option value="'+data[i].f_prole_id+'">'+data[i].f_prole_name+'</option>';
							//else
								//rightStr+='<option value="'+data[i].f_prole_id+'">'+data[i].f_prole_name+'</option>';
						}
						$("#rightRole").append(leftStr);
						//$("#rightRole").append(rightStr);
					}
				});
			}
		});
		$("#orgId").val(f_po_id);
		
		
		$("#contentForm").validate({
			rules: {
				f_pu_name: {
					required: true,
					minlength: 3,
					maxlength: 50
				},
				f_pu_password: {
					required: true,
					minlength: 8,
					pwd: true,
					pwd_independence : true,
					pwd_sort : true
				},
				f_pu_repwd: {
					required: true,
					minlength: 8,
					equalTo: "#f_pu_password"
				},
				email: {
					required: false,
					email: true
				}
			},
			messages: {
				f_pu_name: {
					required: "请输入用户账号",
					minlength: "用户账号不能少于3位",
					maxlength: "用户账号不能超过50位"
				},
				f_pu_password: {
					required: "请输入用户密码",
					minlength: "用户密码不能少于8位",
					ppd: "用户密码必须为8位以上的 至少为数字、大写字母、小写字母、特殊字符的其中3个组合",
					ppd_independence: "密码不能跟账号类似",
					ppd_sort : "密码不能是键盘排序"
				},
				f_pu_repwd: {
					required: "请输入用户确认密码",
					minlength: "用户确认密码不能少于8位",
					equalTo: "用户密码与确认密码需要相同"
				},
				email: "请输入有效的Email地址"
			},
			
		});
		
		
		$.validator.addMethod("ppd",function(value,element,params){
			var ppd = /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_]+$)(?![a-z0-9]+$)(?![a-z\W_]+$)(?![0-9\W_]+$)[a-zA-Z0-9\W_]{8,}$/;
			return this.optional(element)||(ppd.test(value));
		}, "用户密码必须为8位以上的 至少为数字、大写字母、小写字母、特殊字符的其中3个组合");
		
		$.validator.addMethod("ppd",function(value,element,params){
			var f_pu_name = $("#f_pu_name").val();
			if(value.indexOf(f_pu_name) > -1||value.indexOf(f_pu_name.toLocaleUpperCase()) > -1||value.indexOf(f_pu_name.toLowerCase()) > -1||(value.toLowerCase()).indexOf(f_pu_name.toLowerCase()) > -1){
				value = false;
			}
			return this.optional(element)||(value);
		}, "密码不能跟账号类似");
		
		$.validator.addMethod("ppd",function(value,element,params){
			var flag = _isKeyBoardContinuousChar(value);
			return this.optional(element)||(flag);
		}, "密码不能是键盘排序");
	});
	
	function move(obj){
	    if(obj=='')
	    {
	        return;
	    }
	    var leftUser=document.getElementById("leftUser");
	    var rightUser=document.getElementById("rightUser");
		var leftRole=document.getElementById("leftRole");
	    var rightRole=document.getElementById("rightRole");
	    var arrOptions=new Array;
		if(obj=="add2"){
	        for(var i=0;i<leftRole.options.length;i++)
	        {
	            if(leftRole.options[i].selected)
	            {
	            	var arr = $("#f_pour_roleIds").val().split(',');
	            	for(var j=0;j<arr.length;j++){
	            		if(arr[j]==leftRole.options[i].value){
	            			$.messager.alert('提示', '此角色已被授权', 'info', function() {
							});
	            			return false;
	            		}
	            	}
	                arrOptions.push(leftRole.options[i]);
	            }
	        }
	        for(var i=0;i<arrOptions.length;i++)
	        {
	            rightRole.appendChild(arrOptions[i]);
	        }
	    }
	    else if(obj=="delete2"){
	        for(var i=0;i<rightRole.options.length;i++)
	        {
	            if(rightRole.options[i].selected)
	            {
	                arrOptions.push(rightRole.options[i]);    
	            }
	        }
	        for(var i=0;i<arrOptions.length;i++)
	        {
	            leftRole.appendChild(arrOptions[i]);
	        }
	    }
	}

	function _submitFm() {
	  if($("#contentForm").validate().form()) {
			$("#f_pu_pwd").val($.md5($("#f_pu_password").val()));  
			
			var roleList="";
			$("#leftRole option").each(function(){
				roleList+=$(this).val()+",";
			});
			$("#roleIds").val(roleList);
		app_ajax('../user/add_user_org_roles.do', $("#contentForm").serialize(), function(data) {
				if (data.success) {
					$.messager.progress('close');
					$.messager.alert('提示', '提交成功！', 'info', function() {
						parent.$('#addUser').dialog('close');
						parent.gotoSearch($('#easyui-treegrid'));
					});
				} else {
					$.messager.alert('提示', data.result, 'info', function() {
						$.messager.progress('close');
					});
				}
		});
	 }
	}
	
	function authorize(){
		var roleList="";
		var roleListName="";
		$("#leftRole option").each(function(){
			roleList+=$(this).val()+",";
			roleListName +=$(this).text()+",";
		});
		openDialog($('#dlg'),'user_role.jsp?f_role_ids='+roleList+"&f_role_names="+encodeURIComponent(roleListName)+"&f_pour_roleIds="+$("#f_pour_roleIds").val());
	}
	
	//判断是否为键盘排序密码
	function _isKeyBoardContinuousChar(str) {
		var c1 = [
            ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+'],
            ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '|'],
            ['A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"'],
            ['Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?']
           
        ];
        var c2 = [
            ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '='],
            ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\\'],
            ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\''],
            ['z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/']
            
        ];
        str = str.split("");
        //获取坐标位置
        var y = [];
        var x = [];
        for (var c = 0; c < str.length; c++) {
            y[c] = 0;//当做~`键处理
            x[c] = -1;
            for (var i = 0; i < c1.length; i++) {
                for (var j = 0; j < c1[i].length; j++) {
                    if (str[c] == c1[i][j]) {
                        y[c] = i;
                        x[c] = j;
                    }
                }
            }
            if (x[c] != -1) continue;
            for (var i = 0; i < c2.length; i++) {
                for (var j = 0; j < c2[i].length; j++) {
                    if (str[c] == c2[i][j]) {
                        y[c] = i;
                        x[c] = j;
                    }
                }
            }
        }
        //匹配坐标连线
        for (var c = 1; c < str.length - 1; c++) {
            if (y[c - 1] == y[c] && y[c] == y[c + 1]) {
                if ((x[c - 1] + 1 == x[c] && x[c] + 1 == x[c + 1]) || (x[c + 1] + 1 == x[c] && x[c] + 1 == x[c - 1])) {
                	return false;
                }
            } else if (x[c - 1] == x[c] && x[c] == x[c + 1]) {
                if ((y[c - 1] + 1 == y[c] && y[c] + 1 == y[c + 1]) || (y[c + 1] + 1 == y[c] && y[c] + 1 == y[c - 1])) {
                	return false;
                }
            }
        }
        return true;
    }
</script>
</head>

<body class="overfwidth">
	<div class="barnavtop">您所在的位置：用户管理 &gt; 用户账号新增</div>
	<div id="workspace">
		<!--主体 开始-->
		<div id="container">
			<div class="editspace">
				<form id="contentForm" class="cmxform" enctype="multipart/form-data"
					name="formName">
					<input type="hidden" id="orgId" name="orgId" value="0"/>
					<input type="hidden" id="roleIds" name="roleIds" value="" />
					<input type="hidden" name="f_pour_roleIds" id="f_pour_roleIds" value="" />
					<legend>用户账号新增</legend>
					<fieldset>
						<div class="fitem">
							<table width="100%" border="0" cellpadding="0" cellspacing="0">
								<tr>
									<td width="30%" align="right"><span class="red">*</span>用户账号：</td>
									<td width="70%"><input name="f_pu_name" id="f_pu_name"
										style="width: 150px;" value="" class="yl_inp yw_name" /> <span
										id="f_pu_name_tip" class="tip"></span></td>
								</tr>
							</table>
						</div>
					</fieldset>

					<fieldset>
						<div class="fitem">
							<table width="100%" border="0" cellpadding="0" cellspacing="0">
								<tr>
									<td width="30%" align="right"><span class="red">*</span>用户密码：
									</td>
									<td width="70%"><input type="password"
										name="f_pu_password" id="f_pu_password" style="width: 150px;"
										value="" class="yl_inp yw_name" /> <input type="hidden"
										name="f_pu_pwd" id="f_pu_pwd" value="" /></td>
								</tr>
							</table>
						</div>
					</fieldset>

					<fieldset>
						<div class="fitem">
							<table width="100%" border="0" cellpadding="0" cellspacing="0">
								<tr>
									<td width="30%" align="right"><span class="red">*</span>确认用户密码：</td>
									<td width="70%"><input type="password" name="f_pu_repwd"
										id="f_pu_repwd" style="width: 150px;" value=""
										class="yl_inp yw_name" /></td>
								</tr>
							</table>
						</div>
					</fieldset>

					<fieldset>
						<div class="fitem">
							<table width="100%" border="0" cellpadding="0" cellspacing="0">
								<tr>
									<td width="30%" align="right"><span class="red">*</span>用户姓名：</td>
									<td width="70%"><input name="f_pu_realName"
										id="f_pu_realName" style="width: 150px;" value=""
										class="yl_inp yw_name" /> <span id="f_pu_realName_tip"
										class="tip"></span></td>
								</tr>
							</table>
						</div>
					</fieldset>

					<fieldset>
						<div class="fitem">
							<table width="100%" border="0" cellpadding="0" cellspacing="0">
								<tr>
									<td width="30%" align="right">性别：</td>
									<td width="70%"><select name="f_pu_sex" id="f_pu_sex"
										style="width: 150px;">
											<option value="3">未知</option>
											<option value="1">男</option>
											<option value="0">女</option>
									</select> <span id="f_pu_sex_tip" class="tip"></span></td>
								</tr>
							</table>
						</div>
					</fieldset>

					<fieldset>
						<div class="fitem">
							<table width="100%" border="0" cellpadding="0" cellspacing="0">
								<tr>
									<td width="30%" align="right">用户状态：</td>
									<td width="70%"><select name="f_pu_status"
										id="f_pu_status" style="width: 150px;">
											<option value="0">未知</option>
											<option value="1">有效</option>
											<option value="2">无效</option>
									</select> <span id="f_pu_status_tip" class="tip"></span></td>
								</tr>
							</table>
						</div>
					</fieldset>
					
					<fieldset class="hide-field">
						<div class="fitem">
							<table width="100%" border="0" cellpadding="0" cellspacing="0">
								<tr>
									<td width="30%" align="right">角色：</td>
									<td><select id="leftRole" size="5" style="width: 150px;"></select>
									</td>
									<td><input type="button" value=">>" onclick="move('add2')" /><br />
										<input type="button" style="margin-bottom: 10px;"
										value="<<" onclick=" move('delete2')"/></td>
									<td><select id="rightRole" size="5" style="width: 150px;"></select>
									</td>
								</tr>
							</table>
						</div>
					</fieldset>
					
					<fieldset>
						<div class="fitem">
							<table width="100%" border="0" cellpadding="0" cellspacing="0">
								<tr>
									<td width="30%" align="right">角色授权：</td>
									<td width="70%"><input name="f_pour_roleNames"
										id="f_pour_roleNames" readonly="readonly" style="width: 300px;">
									</td>
								</tr>
							</table>
						</div>
					</fieldset>
					<input type="hidden" name="stime" value="<%=nonceObj[0]%>"/>
					<input type="hidden" name="sign" value="<%=nonceObj[1]%>"/>
				</form>
			</div>
			<div class="toolbar" style="text-align: center;">
				<a class="easyui-linkbutton" data-options="iconCls:'icon-ok'"
					href="javascript:void(0);" onclick="_submitFm()">保存</a> <a
					href="javascript:void(0);" data-options="iconCls:'icon-cancel'"
					class="easyui-linkbutton"
					onclick="javascript:parent.$('#addUser').dialog('close')">取消</a>
					<limits:uiAuth url="user_role.jsp">
					<a class="easyui-linkbutton" data-options="iconCls:'icon-ok'"
					href="javascript:void(0);" onclick="javaScript:authorize()">角色授权</a> 
					</limits:uiAuth>
			</div>
		</div>
		<!--主体 结束-->
	</div>
	
	<div id="dlg" class="easyui-dialog"
		style="width: 400px; height:300px; padding-top: 0px;"
		data-options="title:'用户授权', iconCls:'icon-save',modal:true,closed:true,buttons:'#dlg_buttons'">
		<iframe scrolling="auto" frameborder="0"
			style="width: 100%; height: 99%;"></iframe>
	</div>
	</body>
</html>
